package common;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by sshong on 15/7/23.
 */
public class DateUtil {
    /**
     * 自动根据当前时间获取时间字符串
     * @param timestamp
     * @return
     */
    public static String autoFormat(long timestamp) {
        long now = (new Date()).getTime();
        long gap = (now - timestamp) / 1000;
        int minute = 60;
        int hour = 60 * minute;
        int day = 24 * hour;

        if (gap > 0 && gap < hour) {
            double multiper = Math.floor(gap / minute);
            return multiper == 0 ? "刚刚" : ((int)multiper + "分钟前");
        } else if (gap > 0 && gap < day) {
            double multiper = Math.floor(gap / hour);
            return (int)multiper + "小时前";
        } else if (gap > 0 && gap < 2 * day) {
            return "昨天";
        } else if (gap > 0 && gap < 3 * day) {
            return "前天";
        }
        return (new SimpleDateFormat("yyyy-MM-dd")).format(new Date(timestamp));
    }
}
